package com.gitee.hifong45.sub;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.gitee.hifong45.Str;
import com.gitee.hifong45.Xml;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.setting.dialect.Props;

public class SubPrj {
	String NAME_BUILDTIME = "project.build.time";
	String TEXT_BUILDTIME = "${maven.build.timestamp}";
	String PROP_BUILDTIME = "\r\n" + NAME_BUILDTIME + "=@" + NAME_BUILDTIME + "@";

	public static void main(String[] args) {
		SubPrj.setBuildTime();
	}
    public static void setBuildTime() {
		Console.log("开始处理编译时间");
		String pomFileName = "pom.xml";
		// pomFileName = "test.xml";
        String applicationFileName = "application.properties";

        String prjRootDir = System.getProperty("user.dir");
		String srcDir = prjRootDir + "/" + "src";
		String pomFilePath = prjRootDir + "/" + pomFileName;
		String propsFilePath = srcDir + "/main/resources/"  + applicationFileName;

		Console.log("项目目录: {}, pom.xml路径: {}, 配置文件路径: {}", prjRootDir, pomFilePath, propsFilePath);

		SubPrj prj = new SubPrj();
		prj.setPomBuildTime(pomFilePath);
		prj.setPropertiesBuildTime(propsFilePath);
	}

	public void setPropertiesBuildTime(String filePath) {
		Console.log("开始处理properties文件 {}", filePath);
		Props props = new Props(filePath);
		String buildTime = props.getStr("project.build.time");
		if(buildTime == null) {
			Console.log("没有属性：{}，此次新增。", NAME_BUILDTIME);
			// 不用props工具类，因为会直接覆盖原文件，导致一些注释丢失
			FileUtil.appendUtf8String(PROP_BUILDTIME, filePath);
			return;
		}
		Console.log("已有属性：{}，结束。", NAME_BUILDTIME);
	}

	/**
	 * 设置pom.xml里的构建时间节点
	 * @param filePath pom.xml文件绝对路径
	 */
	public void setPomBuildTime(String filePath) {
		Document doc=XmlUtil.readXML(filePath);
		String NAME_NAME = "name";
		String NAME_PROPERTIES = "properties";
		Element elements = doc.getDocumentElement();
		Element eleProperties = XmlUtil.getElement(elements, NAME_PROPERTIES);
		if(eleProperties == null) {
			// 没有 properties 节点
			Console.log("没有properties节点，此次添加properties和buildtime节点");
			// Element nodeProject = XmlUtil.getElement(elements, "project"); // 根project节点
			Element eleRoot = XmlUtil.getRootElement(doc); // 根节点
			Element eleName = XmlUtil.getElement(elements, NAME_NAME); // name 节点

			Element eleBuildTime = Xml.createElementText(doc, NAME_BUILDTIME, TEXT_BUILDTIME);
			Element eleProp = Xml.createElementChild(doc, NAME_PROPERTIES, eleBuildTime);
			Xml.insertAfter(eleRoot, eleProp, eleName); // 在参考节点后添加新节点
			Xml.toFile(doc, filePath, 4);
			return;
		}
		// 有properties 节点，没有build-time节点
		Element eleBuildTime = XmlUtil.getElement(eleProperties, NAME_BUILDTIME);
		if(eleBuildTime == null) {
			Console.log("有properties节点，没有buildtime节点。此次添加buildtime节点");
			eleBuildTime = Xml.createElementText(doc, NAME_BUILDTIME, TEXT_BUILDTIME);
			eleProperties.appendChild(eleBuildTime);
			Xml.toFile(doc, filePath);
			return;
		}
		Console.log("pom文件已有properties和buildtime节点，结束");
	}

	public static ProjectInfo getProjectInfo() {
		ProjectInfo prjInfo = new ProjectInfo();
        prjInfo.active = Str.orElse(SpringUtil.getActiveProfile(), "No setting");
		prjInfo.port = Str.orElse(SpringUtil.getProperty("server.port"), "8080");
		prjInfo.version = Str.orElse(SpringUtil.getProperty("app.version"), "No setting");
		prjInfo.buildTime = SpringUtil.getProperty("project.build.time");
        if(prjInfo.buildTime == null) {
            Console.log("没有配置编译时间。此次进行处理");
            SubPrj.setBuildTime();
        } else {
            prjInfo.buildTime = LocalDateTimeUtil.formatNormal(LocalDateTimeUtil.parse(prjInfo.buildTime.replace("Z", "")).plusHours(8));
        }
		return prjInfo;
    }

}
